package com.lin.mr.maizilaundry.myadapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lin.mr.maizilaundry.R;
import com.lin.mr.maizilaundry.utils.ContactsJean;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by lenovog on 2016/8/8.
 */
public class ContactsAdapter extends BaseAdapter {

    Context context;
    List<ContactsJean.UserInfoBean> contactsList;
    BitmapUtils bitmapUtils;

    public ContactsAdapter(Context context, List<ContactsJean.UserInfoBean> contactsList) {
        this.context = context;
        this.contactsList = contactsList;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        //此时使用了缓存，但是效果仍然不是很好，
        if (convertView == null){
            view = View.inflate(context, R.layout.fragment_contacts_item,null);
        }else {
            view = convertView;
        }



        //ImageLoader初始化
//        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(context)
//                .threadPoolSize(5)
//                .memoryCacheExtraOptions(50, 50).build();
//        ImageLoader.getInstance().init(configuration);

        //ImageLoader配置
//        DisplayImageOptions options = new DisplayImageOptions.Builder()
//                .showImageOnLoading(R.mipmap.error)
//                .cacheOnDisk(true) //启用外存缓存
//                .displayer(new FadeInBitmapDisplayer(100)).build();

        //得到控件
        CircleImageView photo = (CircleImageView) view.findViewById(R.id.iv_photo);
        TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
        TextView tv_word = (TextView) view.findViewById(R.id.tv_word);
        ContactsJean.UserInfoBean contactsInfo = contactsList.get(position);
        //ImageLoader异步加载
//        ImageLoader.getInstance().displayImage(contactsInfo.getHead(),photo,options);
        tv_name.setText(contactsInfo.getName());
        tv_word.setText(contactsInfo.getSignature());
        bitmapUtils = new BitmapUtils(context);
        //设置下载中的默认图片
        bitmapUtils.configDefaultLoadingImage(R.mipmap.error);
        bitmapUtils.display(photo,contactsInfo.getHead());
        return view;
    }

    @Override
    public int getCount() {
        return contactsList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


}
